import axios from 'axios';
import fs from 'fs';

const fileId = '7ryM2G71XRVwkQnkklWrbI';
const nodeId = '0:2753'; // 登录选项页面
const token = process.env.FIGMA_ACCESS_TOKEN;

console.log('🔍 查找华为账号登录图标...');

try {
  const response = await axios.get(`https://api.figma.com/v1/files/${fileId}/nodes`, {
    headers: { 'X-Figma-Token': token },
    params: { ids: nodeId }
  });
  
  const nodeData = response.data.nodes[nodeId];
  
  // 查找华为登录按钮中的图标
  function findHuaweiIcon(node, path = '') {
    if (node.name && node.name.includes('华为') || node.name.includes('Huawei')) {
      console.log(`找到华为相关元素: ${path}/${node.name} (${node.type})`);
      
      if (node.children) {
        node.children.forEach(child => {
          if (child.type === 'VECTOR' || child.type === 'GROUP') {
            console.log(`  图标元素: ${child.name} (${child.type}) - ID: ${child.id}`);
          }
        });
      }
    }
    
    if (node.children) {
      node.children.forEach(child => {
        findHuaweiIcon(child, path + '/' + node.name);
      });
    }
  }
  
  if (nodeData && nodeData.document) {
    findHuaweiIcon(nodeData.document);
  }

  // 尝试获取按钮区域的图标
  const buttonNodes = ['0:2881', '0:2809']; // 可能的按钮节点ID
  
  for (const btnId of buttonNodes) {
    try {
      const btnResponse = await axios.get(`https://api.figma.com/v1/images/${fileId}`, {
        headers: { 'X-Figma-Token': token },
        params: {
          ids: btnId,
          format: 'png',
          scale: 3
        }
      });

      const imageUrl = btnResponse.data.images[btnId];
      if (imageUrl) {
        const imgResponse = await axios.get(imageUrl, { responseType: 'arraybuffer' });
        fs.writeFileSync(`../entry/src/main/resources/rawfile/huawei_icon_${btnId.replace(':', '_')}.png`, imgResponse.data);
        console.log(`✅ 华为图标已下载: huawei_icon_${btnId.replace(':', '_')}.png`);
      }
    } catch (error) {
      console.log(`节点 ${btnId} 获取失败`);
    }
  }

} catch (error) {
  console.log('❌ 获取失败:', error.response?.status, error.response?.statusText);
}
